package com.yunshipei.ui.fragment;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yunshipei.common.Globals;
import com.yunshipei.common.manager.EnterConfig;
import com.yunshipei.enterplorer.BuildConfig;
import com.yunshipei.enterplorer.R;
import com.yunshipei.manager.UpdateManager;
import com.yunshipei.manager.YspPreferences;
import com.yunshipei.model.MainExtraBean;
import com.yunshipei.model.YspEvent;
import com.yunshipei.ui.activity.AboutDetailActivity;
import com.yunshipei.utils.BaseUtil;
import com.yunshipei.utils.ToastUtils;

import butterknife.Bind;
import io.rong.eventbus.EventBus;

public class AboutFragment extends BaseBodyFragment implements View.OnClickListener {

    public static AboutFragment newInstance(MainExtraBean mainExtraBean) {
        AboutFragment aboutFragment = new AboutFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(ARGS_CUSTOMER_MAIN_DATA, mainExtraBean);
        aboutFragment.setArguments(bundle);
        return aboutFragment;
    }

    public static final int RESULT_CODE = 62235;
    public static final String EXTRA_RESULT_DATA = "developer.result.data";

    @Bind(R.id.tv_enterplorer_logo)
    protected TextView mLogo;
    @Bind(R.id.ll_about_container)
    protected LinearLayout mParentContainer;
    @Bind(R.id.tv_title)
    protected TextView tvTitle;

    private boolean isDebug;

    @Override
    public int viewLayout() {
        return R.layout.fragment_about;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tvTitle.setBackgroundColor(EnterConfig.getInstance().getGlobalColor());
        tvTitle.setText(getString(R.string.about_info));
        tvTitle.setVisibility(isPad() ? View.VISIBLE : View.GONE);
        String version = BaseUtil.getVersionName();
        mLogo.setText(BuildConfig.DEBUG ? "内测版" + version : version);
        initLogoClickEvent();
        Item[] items = Item.values();
        for (int i = 0; i < items.length; i++) {
            View container = mParentContainer.getChildAt(i + 2);
            TextView tv = (TextView) container.findViewById(R.id.tv_item_content);
            tv.setText(items[i].name);
            container.setTag(items[i]);
            container.setOnClickListener(this);
        }
    }

    private void initLogoClickEvent() {
        final SharedPreferences preferences = YspPreferences.getInstance().getSharedPreferences();
        isDebug = preferences.getBoolean(Globals.YSP_DEBUG, false);
        mLogo.setOnClickListener(new View.OnClickListener() {
            long[] mHints = new long[6];

            @SuppressLint("CommitPrefEdits")
            @Override
            public void onClick(View view) {
                System.arraycopy(mHints, 1, mHints, 0, mHints.length - 1);
                mHints[mHints.length - 1] = SystemClock.uptimeMillis();
                if (mHints[0] != 0 && SystemClock.uptimeMillis() - mHints[0] <= 1500) {
                    if (isDebug) {//关闭开发者模式，并且删除log文件夹下的文件
                        isDebug = false;
                        ToastUtils.showToast("关闭开发者模式");
                        developModeChange(false);
                    } else {//打开开发者模式，使能查看log文件夹下的文件
                        isDebug = true;
                        ToastUtils.showToast("开启开发者模式");
                        developModeChange(true);
                    }
                    preferences.edit().putBoolean(Globals.YSP_DEBUG, isDebug).apply();
                    for (int i = 0; i < mHints.length; i++) {
                        mHints[i] = 0;
                    }
                }
            }
        });
    }

    private void developModeChange(boolean result) {
        if (isPad()) {
            EventBus.getDefault().post(new YspEvent.DevelopModeChange(result));
        } else {
            getActivity().setResult(RESULT_CODE, new Intent().putExtra(EXTRA_RESULT_DATA, result));
            getActivity().finish();
        }
    }

    @Override
    public void onClick(View v) {
        Item tag = (Item) v.getTag();
        switch (tag) {
            case UPDATE:
                UpdateManager updateManager = new UpdateManager(mContext, true);
                updateManager.checkUpdate(mMainExtraBean.getUserInfo().getCompanyId());
                break;
            case FUNCTION:
            case SERVICE_PROTOCOL:
            case POLICY:
                if (isPad()) {
                    EventBus.getDefault().post(new YspEvent.AboutDetailClick(AboutDetailFragment.class.getName(), tag.name, tag.url));
                } else {
                    startActivity(AboutDetailActivity.getHandlerIntent(mContext, tag.name, tag.url));
                }
                break;
            default:
                break;
        }
    }

    private enum Item {
        UPDATE("检查新版本", ""),
        FUNCTION("功能介绍", "https://hub.enterplorer.com/feature_android.html"),
        SERVICE_PROTOCOL("服务协议", "https://hub.enterplorer.com/protocal.html"),
        POLICY("隐私政策", "https://hub.enterplorer.com/privacy.html");

        private String name;
        private String url;

        Item(String name, String url) {
            this.name = name;
            this.url = url;
        }
    }
}
